package com.niiwoo.civet.account.service.local.openAccount2.service;

import com.niiwoo.civet.account.constant.AccountRegisterErrorCode;
import com.niiwoo.civet.account.dao.entity.OpenAccountOrder;
import com.niiwoo.civet.account.dao.entity.OpenAccountRecord;
import com.niiwoo.civet.account.enums.AccountBaseIsShowBillEnum;
import com.niiwoo.civet.account.enums.AccountTypeEnum;
import com.niiwoo.civet.account.enums.OpenAccountRecordStatusEnum;
import com.niiwoo.civet.account.service.local.openAccount2.dto.AccountInfo;
import com.niiwoo.civet.account.service.local.openAccount2.dto.CreateRequest;
import com.niiwoo.civet.account.service.local.openAccount2.dto.RegisterUpdateRequest;
import com.niiwoo.civet.account.service.local.openAccount2.dto.UpdateRequest;
import com.niiwoo.civet.account.service.local.openAccount2.type.RegisterTypeable;
import com.niiwoo.civet.base.constant.OrgConstant;
import com.niiwoo.tripod.provider.exception.BizException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component
public class RegisterOpenAccountService extends AbstractOpenAccountService<OpenAccountOrder> implements RegisterTypeable {

    @Override
    protected void innerCreateParamValid(CreateRequest request) {

    }

    @Override
    protected void innerCreateRecordValid(CreateRequest request, List<OpenAccountRecord> recordList) {
        // 其他角色开户记录
        recordList.stream()
                .filter(r -> !Objects.equals(r.getAccountRole(), request.getRole().getValue()))
                .findFirst()
                .ifPresent(r -> {
                    switch (OpenAccountRecordStatusEnum.of(r.getStatus())) {
                        case PROCESSING:
                            throw new BizException(AccountRegisterErrorCode.HAS_OTHER_PROCESSING_ACCOUNT);
                        case AUDITING:
                            throw new BizException(AccountRegisterErrorCode.HAS_OTHER_AUDITING_ACCOUNT);
                        case AUDIT_RETURN:
                            throw new BizException(AccountRegisterErrorCode.HAS_OTHER_AUDIT_RETURN_ACCOUNT);
                        case SUCCESS:
                            throw new BizException(AccountRegisterErrorCode.HAS_OTHER_REGISERED_ACCOUNT);
                    }
                });
    }

    @Override
    protected AccountInfo innerUpdateAccountInfo(OpenAccountRecord record, OpenAccountOrder order, UpdateRequest request) {
        RegisterUpdateRequest registerUpdateRequest = (RegisterUpdateRequest) request;
        AccountInfo accountInfo = new AccountInfo();
        accountInfo.setAccountNo(record.getAccountNo());
        accountInfo.setRole(record.getAccountRole());
        accountInfo.setUserId(record.getUserId());
        accountInfo.setName(order.getRealName());
        accountInfo.setIdCardNo(order.getCertNo());
        accountInfo.setBankCardNo(order.getBindCardNo());
        accountInfo.setBankCardMobileNo(order.getMobileNo());
        accountInfo.setBankCode(registerUpdateRequest.getBankCode().getCode());
        accountInfo.setAuthList(order.getAuthList());
        accountInfo.setAccountType(AccountTypeEnum.PERSONAL);
        accountInfo.setOrgId(order.getOrgId());
        accountInfo.setIsShowBill(isNiiWooAccount(order.getOrgId())? AccountBaseIsShowBillEnum.YES.getValue():AccountBaseIsShowBillEnum.NO.getValue());
        return accountInfo;
    }
}
